package com.itheima.utils;

/**
 * @ClassName: IpLongUtils
 * @Description:
 * @author: Yrd
 * @date: 2021-10-07 14:37
 */
public class IpLongUtils {
    /**
     *
     * @Title : ip2Long
     * @Description : 把字符串IP转换成long

     * @param ipStr 字符串IP
     * @return long IP对应的long值
     *
     * @user : Yrd
     * @date : 2021-10-07 14:46
     */
    public static long ip2Long(String ipStr){
        String[] ip = ipStr.split("\\.");
        return (Long.valueOf(ip[0])<<24)+(Long.valueOf(ip[1])<<16)+
                (Long.valueOf(ip[2])<<8)+Long.valueOf(ip[3]);
    }

    /**
     *
     * @Title : long2Ip
     * @Description : 把IP的long值转换成字符串

     * @param ipLong IP的long值
     * @return java.lang.String long值对应的字符串
     *
     * @user : Yrd
     * @date : 2021-10-07 14:52
     */
    public static String long2Ip(long ipLong){
        StringBuilder ip = new StringBuilder();
        ip.append(ipLong>>>24).append(".");
        ip.append((ipLong>>>16)&0xFF).append(".");
        ip.append((ipLong>>>8)&0xFF).append(".");
        ip.append(ipLong&0xFF);
        return ip.toString();
    }


    public static void main(String[] args){
        System.out.println(ip2Long("192.168.27.1"));
        System.out.println(long2Ip(3232242434L));
    }
}
